package com.changhai.diabetes.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class DateConvertor {
	private static Map<String, DateFormat> formatorMap = new HashMap<String, DateFormat>();
	private static String defaultPatten = "yyyy-MM-dd HH:mm:ss";

	/**
	 * format date ,if patten is null,use default patten .
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date, String patten) {
		if (date == null) {
			return "";
		}
		if (patten == null || patten.length() <= 0) {
			patten = defaultPatten;
		}
		DateFormat f = getDateFormator(patten);
		return f.format(date);
	}

	public static String formatDate(Long date, String patten) {
		if (date == null) {
			return "";
		}
		if (patten == null || patten.length() <= 0) {
			patten = defaultPatten;
		}
		DateFormat f = getDateFormator(patten);
		return f.format(date);
	}

	/**
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return formatDate(date, defaultPatten);
	}

	public static String formatDate(Long date) {
		return formatDate(date, defaultPatten);
	}

	/**
	 * get pooling formator
	 * 
	 * @param patten
	 * @return
	 */
	private static DateFormat getDateFormator(String patten) {
		if (formatorMap == null) {
			formatorMap = new HashMap<String, DateFormat>();
		}
		if (!formatorMap.containsKey(patten)) {
			DateFormat f = new SimpleDateFormat(patten);
			formatorMap.put(patten, f);
		}
		return formatorMap.get(patten);
	}

	/**
	 * yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static Date getSimpleDate(Date date) {
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.clear(Calendar.MILLISECOND);
			calendar.clear(Calendar.SECOND);
			calendar.clear(Calendar.MINUTE);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			return calendar.getTime();
		}
		return null;
	}

	/**
	 * 
	 * @param ds
	 * @return
	 * @throws BaseException
	 */
	public static Date parseDate(String ds) {
		return parseDate(ds, defaultPatten);
	}

	/**
	 * 
	 * @param ds
	 * @return
	 * @throws BaseException
	 */
	public static Date parseDate(String ds, String patten) {
		if (patten == null || patten.length() <= 0) {
			patten = defaultPatten;
		}
		DateFormat df = new SimpleDateFormat(patten);
		try {
			return df.parse(ds);
		} catch (ParseException e) {
			// log.errorf("Unable to parse date {%s} in {%s} : \r\n {%s} ", ds,
			// patten, e);
		}
		return null;
	}

	/**
	 * 
	 * @param dt
	 * @param years
	 * @return
	 */
	public static Date addYear(Date dt, int years) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.YEAR, years);
		return thisday.getTime();
	}

	/**
	 * 
	 * @param dt
	 * @param m
	 * @return
	 */
	public static Date addMonth(Date dt, int m) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.MONTH, m);
		return thisday.getTime();
	}

	/**
	 * 
	 * @param dt
	 * @param d
	 * @return
	 */
	public static Date addDate(Date dt, int d) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.DATE, d);
		return thisday.getTime();
	}

	/**
	 * 
	 * @param dt
	 * @param h
	 * @return
	 */
	public static Date addHour(Date dt, int h) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.HOUR_OF_DAY, h);
		return thisday.getTime();
	}

	/**
	 * 
	 * @param dt
	 * @param m
	 * @return
	 */
	public static Date addMinute(Date dt, int m) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.MINUTE, m);
		return thisday.getTime();
	}

	/**
	 * 
	 * @param dt
	 * @param s
	 * @return
	 */
	public static Date addSecond(Date dt, int s) {
		GregorianCalendar thisday = new GregorianCalendar();
		thisday.setTime(dt);
		thisday.add(GregorianCalendar.SECOND, s);
		return thisday.getTime();
	}

	/**
	 * 获得long time
	 * 
	 * @return
	 */
	public static long getDateLongTime() {
		return parseDate(getShortDate(), defaultPatten).getTime();
	}
	
	public static String getShortDate() {
		DateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return d.format(new Date());
	}
	
	public static String getCurrentDate(String fmt) {
		DateFormat d = new SimpleDateFormat(fmt);
		return d.format(new Date());
	}
}
